home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
ed_el.zip
/
ED.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-02-15
|
44KB
|
2,223 lines
/*
* ed - standard editor
* ~~
* Authors: Brian Beattie, Kees Bot, and others
*
* Copyright 1987 Brian Beattie Rights Reserved.
* Permission to copy or distribute granted under the following conditions:
* 1). No charge may be made other than reasonable charges for reproduction.
* 2). This notice must remain intact.
* 3). No further restrictions may be added.
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* TurboC mods and cleanup 8/17/88 RAMontante.
* Further information (posting headers, etc.) at end of file.
* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
*/
int version = 4; /* used only in the "set" function, for i.d. */
#include <stdio.h>
#ifdef __TURBOC__
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <io.h>
#define _cleanup() ;
#endif /* ifdef __TURBOC__ */
/*
* #defines for non-printing ASCII characters
*/
#define NUL 0x00 /* ^@ */
#define EOS 0x00 /* end of string */
#define SOH 0x01 /* ^A */
#define STX 0x02 /* ^B */
#define ETX 0x03 /* ^C */
#define EOT 0x04 /* ^D */
#define ENQ 0x05 /* ^E */
#define ACK 0x06 /* ^F */
#define BEL 0x07 /* ^G */
#define BS 0x08 /* ^H */
#define HT 0x09 /* ^I */
#define LF 0x0a /* ^J */
#define NL '\n'
#define VT 0x0b /* ^K */
#define FF 0x0c /* ^L */
#define CR 0x0d /* ^M */
#define SO 0x0e /* ^N */
#define SI 0x0f /* ^O */
#define DLE 0x10 /* ^P */
#define DC1 0x11 /* ^Q */
#define DC2 0x12 /* ^R */
#define DC3 0x13 /* ^S */
#define DC4 0x14 /* ^T */
#define NAK 0x15 /* ^U */
#define SYN 0x16 /* ^V */
#define ETB 0x17 /* ^W */
#define CAN 0x18 /* ^X */
#define EM 0x19 /* ^Y */
#define SUB 0x1a /* ^Z */
#define ESC 0x1b /* ^[ */
#define FS 0x1c /* ^\ */
#define GS 0x1d /* ^] */
#define RS 0x1e /* ^^ */
#define US 0x1f /* ^_ */
#define SP 0x20 /* space */
#define DEL 0x7f /* DEL*/
/* Definitions of meta-characters used in pattern matching
* routines. LITCHAR & NCCL are only used as token identifiers;
* all the others are also both token identifier and actual symbol
* used in the regular expression.
*/
#define BOL '^'
#define EOL '$'
#define ANY '.'
#define LITCHAR 'L'
#define ESCAPE '\\'
#define CCL '[' /* Character class: [...] */
#define CCLEND ']'
#define NEGATE '~'
#define NCCL '!' /* Negative character class [^...] */
#define CLOSURE '*'
#define OR_SYM '|'
#define DITTO '&'
#define OPEN '('
#define CLOSE ')'
/* Largest permitted size for an expanded character class. (i.e. the class
* [a-z] will expand into 26 symbols; [a-z0-9] will expand into 36.)
*/
#define CLS_SIZE 128
#define TRUE 1
#define FALSE 0
#define ERR -2
#define FATAL (ERR-1)
#define CHANGED (ERR-2)
#define SET_FAIL (ERR-3)
#define SUB_FAIL (ERR-4)
#define BUFFER_SIZE 2048 /* stream-buffer size: == 1 hd cluster */
#define LINFREE 1 /* entry not in use */
#define LGLOB 2 /* line marked global */
#define MAXLINE 512 /* max number of chars per line */
#define MAXPAT 256 /* max number of chars per replacement pattern */
#define MAXFNAME 256 /* max file name size */
/** Global variables **/
/* Tokens are used to hold pattern templates. (see makepat()) */
typedef char BITMAP;
typedef struct token {
char tok;
char lchar;
BITMAP *bitmap;
struct token *next;
} TOKEN;
#define TOKSIZE sizeof (TOKEN)
struct line {
int l_stat; /* empty, mark */
struct line *l_prev;
struct line *l_next;
char l_buff[1];
};
typedef struct line LINE;
int diag = 1; /* diagnostic-output? flag */
char *paropen[9];
char *parclose[9];
int between, parnum;
int truncflg = 1; /* truncate long line flag */
int eightbit = 1; /* save eighth bit */
int nonascii; /* count of non-ascii chars read */
int nullchar; /* count of null chars read */
int truncated; /* count of lines truncated */
char fname[MAXFNAME];
int fchanged; /* file-changed? flag */
int nofname;
int mark['z'-'a'+1];
TOKEN *oldpat;
LINE Line0;
int CurLn = 0;
int LastLn = 0;
char inlin[MAXLINE];
int pflag;
int Line1, Line2, nlines;
int nflg; /* print line number flag */
int lflg; /* print line in verbose mode */
int pflg; /* print current line after each command */
char *inptr; /* tty input buffer */
struct tbl {
char *t_str;
int *t_ptr;
int t_val;
} *t, tbl[] = {
"number", &nflg, TRUE,
"nonumber", &nflg, FALSE,
"list", &lflg, TRUE,
"nolist", &lflg, FALSE,
"eightbit", &eightbit, TRUE,
"noeightbit", &eightbit, FALSE,
0
};
/*-------------------------------------------------------------------------*/
#ifdef __TURBOC__ /* prototypes (unneeded?) */
void prntln(char *, int, int);
void putcntl(char , FILE *);
int doprnt(int, int);
LINE *getptr(int);
BITMAP *makebitmap(unsigned);
void relink(LINE *, LINE *, LINE *, LINE *);
int del(int, int);
int ins(char *);
char *gettxt(int);
int append(int, int);
char *catsub(char *, char *, char *, char *, char *);
char *matchs(char *, TOKEN *, int);
int deflt(int, int);
char *maksub(char *sub, int subsz);
TOKEN *optpat(void);
#else /* !__TURBOC__ */
extern char *strcpy();
extern int *malloc();
extern LINE *getptr();
BITMAP *makebitmap();
extern char *gettxt();
extern char *catsub();
extern char *matchs();
char *maksub();
TOKEN *optpat();
#endif /* __TURBOC__ */
/*________ Macros ________________________________________________________*/
#ifndef max
# define max(a,b) ((a) > (b) ? (a) : (b))
#endif
#ifndef min
# define min(a,b) ((a) < (b) ? (a) : (b))
#endif
#ifndef toupper
# define toupper(c) ((c >= 'a' && c <= 'z') ? c-32 : c )
#endif
/* getpat -- Translate arg into a TOKEN string */
#define getpat(arg) makepat((arg), '\000')
#define nextln(l) ((l)+1 > LastLn ? 0 : (l)+1)
#define prevln(l) ((l)-1 < 0 ? LastLn : (l)-1)
#define clrbuf() del(1, LastLn)
#define Skip_White_Space {while (*inptr==SP || *inptr==HT) inptr++;}
/*________ functions ______________________________________________________*/
/*****************************************************************************
* BITMAP.C - makebitmap, setbit, testbit
* bit-map manipulation routines.
*
* Copyright (c) Allen I. Holub, all rights reserved. This program may
* for copied for personal, non-profit use only.
*/
BITMAP *makebitmap( size )
unsigned size;
{
/* Make a bit map with "size" bits. The first entry in
* the map is an "unsigned int" representing the maximum
* bit. The map itself is concatenated to this integer.
* Return a pointer to a map on success, 0 if there's
* not enough memory.
*/
unsigned *map, numbytes;
numbytes = (size >> 3) + ((size & 0x07) ? 1 : 0 );
#ifdef DEBUG
printf("Making a %u bit map (%u bytes required)\n", size, numbytes);
#endif
if( map = (unsigned *) malloc( numbytes + sizeof(unsigned) ))
*map = size;
return ((BITMAP *)map);
}
setbit( c, map, val )
unsigned c, val;
char *map;
{
/* Set bit c in the map to val.
* If c > map-size, 0 is returned, else 1 is returned.
*/
if( c >= *(unsigned *)map ) /* if c >= map size */
return 0;
map += sizeof(unsigned); /* skip past size */
if( val )
map[c >> 3] |= 1 << (c & 0x07);
else
map[c >> 3] &= ~(1 << (c & 0x07));
return( 1 );
}
testbit( c, map )
unsigned c;
char *map;
{
/* Return 1 if the bit corresponding to c in map is set.
* 0 if it is not.
*/
if( c >= *(unsigned *)map )
return 0;
map += sizeof(unsigned);
return(map[ c >> 3 ] & (1 << (c & 0x07)));
}
/********* end of BITMAP.C ************************************************/
/* omatch.c
*
* Match one pattern element, pointed at by pat, with the character at
* **linp. Return non-zero on match. Otherwise, return 0. *Linp is
* advanced to skip over the matched character; it is not advanced on
* failure. The amount of advance is 0 for patterns that match null
* strings, 1 otherwise. "boln" should point at the position that will
* match a BOL token.
*/
omatch(linp, pat, boln)
char **linp;